﻿/*********************弘石项目三组************************
*  功能描述：客户信息列表控制器
*  作者：刘伟康
*  时间：2021-12-10
*  修改人：刘伟康
*  修改时间：2021-12-10
*********************弘石项目三组************************/
using Hong.Business.Business_ContractManagement;
using Hong.Model.Dto.Model_CustomerManagementDto;
using Hong.Model.SysDomain.Model_CustomerManagement;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MiniExcelLibs;
using System.IO;

namespace WeHongshibApI.Controllers.Controllers_CustomerManagement
{
    [ApiExplorerSettings(GroupName = "Customer")]
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class CustInfoListController : ControllerBase
    {
        #region 构造器注入
        private readonly CustInfoListBusiness _custInfo;
        public CustInfoListController(CustInfoListBusiness custInfo)
        {
            _custInfo = custInfo;
        }
        #endregion

        #region 客户信息列表
        #region 客户信息显示查询
        /// <summary>
        /// 客户信息显示查询
        /// </summary>
        /// <param name="customer"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetCustomerExtendedDto(string custId, string custName, string custPhones, string edTypeId, string edIndustryId, int pageIndex = 1, int pageSize = 2)
        {
            int totalCount;
            var list = _custInfo.GetCustomerExtendedDto(custId, custName, custPhones, edTypeId, edIndustryId, pageIndex, pageSize, out totalCount);
            return Ok(new { list, totalCount });
        }

        /// <summary>
        /// 批量删除客户信息
        /// </summary>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult PDeleteCustomer(string ids)
        {
            return Ok(_custInfo.PDeleteCustomer(ids));
        }

        /// <summary>
        /// 修改客户列表信息
        /// </summary>
        /// <param name="customer"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult PutCustomer(Customer customer)
        {
            return Ok(_custInfo.PutCustomer(customer));
        }
        #endregion

        #region 反填客户信息列表
        /// <summary>
        /// 反填客户列表信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetCustomerFT(string id)
        {
            return Ok(_custInfo.GetCustomerFT(id));
        }
        /// <summary>
        /// 反填扩展信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetSatisfactionFT(string id)
        {
            return Ok(_custInfo.GetSatisfactionFT(id));
        }
        #endregion

        #region 导出excel
        [HttpGet]
        public IActionResult DownloadExcel()
        {
            int totalCount;
            var list = _custInfo.GetCustomerExtendedDto(null, null, null, null, null, 1, 1000000, out totalCount);
            var values = new List<Dictionary<string, string>>();
            foreach (var item in list)
            {
                values.Add(new Dictionary<string, string> {
                    {"客户编号",$"{item.CustId}" }, {"客户名称",$"{item.CustName}" }, {"邮政编码",$"{item.CustEmail}" },
                    {"公司电话",$"{item.CustPhone}" }, {"客户简称",$"{item.CustAbbreviation}" }, {"公司地址",$"{item.CustAddress}" },
                    {"联系人",$"{item.CustPhones}" }, {"传真号",$"{item.CustFaxNumber}" }, {"主键",$"{item.EdId}" },
                    {"客户英文名",$"{item.EdName}" }, {"是否合作",$"{item.EdIsheZuo}" }, {"所在国家",$"{item.EdCountryId1}" },
                    {"所在省份",$"{item.EdCountryId2}" }, {"所在城市",$"{item.EdCountryId3}" }, {"客户类型",$"{item.EdTypeId}" },
                    {"所属行业",$"{item.EdIndustryId}" }, {"邮箱",$"{item.EdEmail}" }, {"关系建立时间",$"{item.EdCreateTime}" },
                    {"关系部门",$"{item.EdBuMen}" }, {"开启银行名称",$"{item.EdBankName}" }, {"企业代码",$"{item.EdEnterprise}" },
                    {"开启银行账号",$"{item.EdBankAccount}" }, {"备注",$"{item.EdBeiZhu}" }, {"客户基本信息（外键）",$"{item.CustomerId}" },
                    {"字典主键",$"{item.DicId}" }, {"字典名称",$"{item.DicName}" }, {"字典主键1",$"{item.DicId1}" },
                    {"字典名称1",$"{item.DicName1}" }
                });
            }
            var memoryStream = new MemoryStream();
            memoryStream.SaveAs(values);
            memoryStream.Seek(0, SeekOrigin.Begin);
            return new FileStreamResult(memoryStream, "applicatio/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
            {
                FileDownloadName = "客户信息.xlsx"
            };
        }


        #endregion
        #endregion


    }
}
